// 自动生成模板EpidemicUser
package epidemic

import (
	"encoding/base64"
	"gitee.com/baal1990/epidemic/global"
	"gitee.com/baal1990/epidemic/utils/crypto"
	"gorm.io/gorm"
	"reflect"
	"time"
)

// EpidemicUser 结构体
/**
上报的和搜索的分开，因为form中字段不一样
*/
type EpidemicUserSearch struct {
	ID                       int        `gorm:"primarykey" json:"id" form:"id"` // 主键ID
	Name                     string     `json:"name" form:"name" gorm:"column:name;comment:姓名;size:50;"`
	Sex                      string     `json:"sex" form:"sex" gorm:"column:sex;comment:性别;"`
	Mobile                   string     `json:"mobile" form:"mobile" gorm:"column:mobile;comment:手机号;size:20;"`
	BirthDate                string     `json:"birthDate" form:"birthDate" gorm:"column:birth_date;comment:生日;"`
	Company                  string     `json:"company" form:"company" gorm:"column:company;comment:公司;size:50;"`
	Department               string     `json:"department" form:"department" gorm:"column:department;comment:部门;size:50;"`
	Organization             string     `json:"organization" form:"organization" gorm:"column:organization;comment:编制;size:50;"`
	Location                 string     `json:"location" form:"location" gorm:"column:location;comment:常驻工作地;size:50;"`
	ResidentialAddressCode   string     `json:"residentialAddressCode" form:"residentialAddressCode" gorm:"column:residential_address_code;comment:居住地址省市区行政编码：101/12/3;size:200;"`
	ResidentialAddressDetail string     `json:"residentialAddressDetail" form:"residentialAddressDetail" gorm:"column:residential_address_detail;comment:居住地址 详细地址：xxx街道xx小区;size:200;"`
	VaccinationRecord        string     `json:"vaccinationRecord" form:"vaccinationRecord" gorm:"column:vaccination_record;comment:疫苗接种记录：字典值;size:50;"`
	VaccinationTime          string     `json:"vaccinationTime" form:"vaccinationTime" gorm:"column:vaccination_time;comment:疫苗接种记录时间：json;size:100;"`
	ReasonForNonVaccination  string     `json:"reasonForNonVaccination" form:"reasonForNonVaccination" gorm:"column:reason_for_non_vaccination;comment:未接种完全原因：字典值;size:50;"`
	Role                     string     `json:"role" form:"role" gorm:"column:role;comment:角色：字典值;size:50;"`
	Status                   string     `json:"status" form:"status" gorm:"column:status;comment:审核状态：字典值;size:50;"`
	ApprovedBy               string     `json:"approvedBy" form:"approvedBy" gorm:"column:approved_by;comment:审批人;size:50;"`
	ApprovalComments         string     `json:"approvalComments" form:"approvalComments" gorm:"column:approval_comments;comment:审批意见;size:200;"`
	ApprovalDate             *time.Time `json:"approvalDate" form:"approvalDate" gorm:"column:approval_date;comment:审批日期;"`
	Label                    string     `json:"label" form:"label"`
}
type EpidemicUser struct {
	ID                       int        `gorm:"primarykey" json:"id" form:"id"` // 主键ID
	Name                     string     `json:"name" form:"name" gorm:"column:name;comment:姓名;size:50;" dfitcencode:"aes"`
	Sex                      string     `json:"sex" form:"sex" gorm:"column:sex;comment:性别;"`
	Mobile                   string     `json:"mobile" form:"mobile" gorm:"column:mobile;comment:手机号;size:20;"`
	BirthDate                string     `json:"birthDate" form:"birthDate" gorm:"column:birth_date;comment:生日;"`
	Company                  string     `json:"company" form:"company" gorm:"column:company;comment:公司;size:50;"`
	Department               string     `json:"department" form:"department" gorm:"column:department;comment:部门;size:50;"`
	Organization             string     `json:"organization" form:"organization" gorm:"column:organization;comment:编制;size:50;"`
	Location                 string     `json:"location" form:"location" gorm:"column:location;comment:常驻工作地;size:50;"`
	ResidentialAddressCode   string     `json:"residentialAddressCode" form:"residentialAddressCode" gorm:"column:residential_address_code;comment:居住地址省市区行政编码：101/12/3;size:200;" dfitcencode:"aes"`
	ResidentialAddressName   string     `json:"residentialAddressName" form:"residentialAddressName" gorm:"column:residential_address_name;comment:居住地址省市区行政名称：山东省/临沂市/河东区;size:200;" dfitcencode:"aes"`
	ResidentialAddressDetail string     `json:"residentialAddressDetail" form:"residentialAddressDetail" gorm:"column:residential_address_detail;comment:居住地址 详细地址：xxx街道xx小区;size:200;" dfitcencode:"aes"`
	VaccinationRecord        string     `json:"vaccinationRecord" form:"vaccinationRecord" gorm:"column:vaccination_record;comment:疫苗接种记录：字典值;size:50;"`
	VaccinationTime          string     `json:"vaccinationTime" form:"vaccinationTime" gorm:"column:vaccination_time;comment:疫苗接种记录时间：json;size:100;"`
	ReasonForNonVaccination  string     `json:"reasonForNonVaccination" form:"reasonForNonVaccination" gorm:"column:reason_for_non_vaccination;comment:未接种完全原因：字典值;size:50;"`
	Role                     string     `json:"role" form:"role" gorm:"column:role;comment:角色：字典值;size:50;"`
	Status                   string     `json:"status" form:"-" gorm:"column:status;comment:审核状态：字典值;size:50;"`
	ApprovedBy               string     `json:"approvedBy" form:"-" gorm:"column:approved_by;comment:审批人;size:50;"`
	ApprovalComments         string     `json:"approvalComments" form:"-" gorm:"column:approval_comments;comment:审批意见;size:200;"`
	ApprovalDate             *time.Time `json:"approvalDate" form:"-" gorm:"column:approval_date;comment:审批日期;" `
	Label                    string     `json:"label" form:"label" gorm:"column:label;comment:标签;"`
}

// TableName EpidemicUser 表名
func (EpidemicUser) TableName() string {
	return "epidemic_user"
}

// 保存前加密数据
func (diary *EpidemicUser) BeforeSave(db *gorm.DB) (err error) {
	v := reflect.ValueOf(diary).Elem() // the struct variable
	for i := 0; i < v.NumField(); i++ {
		fieldInfo := v.Type().Field(i) // a reflect.StructField
		tag := fieldInfo.Tag           // a reflect.StructTag
		name := tag.Get("dfitcencode")
		if name == "aes" {
			value := v.FieldByName(fieldInfo.Name).String()
			//fmt.Println("xxxx", name)
			//fmt.Println("fieldInfo.Name:", fieldInfo.Name)
			//fmt.Println("fieldInfo.Value:", value)
			rs := crypto.MysqlAESEncrypt([]byte(value), []byte(global.ENCRYPTKEY_AES))
			dataByte := base64.StdEncoding.EncodeToString(rs) // 不可用于http传输，否则使用URLEncoding
			v.FieldByName(fieldInfo.Name).Set(reflect.ValueOf(string(dataByte)))
		}
	}
	return
}

// 返回前解密数据
func (diary *EpidemicUser) AfterFind(tx *gorm.DB) (err error) {
	v := reflect.ValueOf(diary).Elem() // the struct variable
	for i := 0; i < v.NumField(); i++ {
		fieldInfo := v.Type().Field(i) // a reflect.StructField
		tag := fieldInfo.Tag           // a reflect.StructTag
		name := tag.Get("dfitcencode")
		if name == "aes" {
			value := v.FieldByName(fieldInfo.Name).String()
			//fmt.Println("xxxx", name)
			//fmt.Println("fieldInfo.Name:", fieldInfo.Name)
			//fmt.Println("fieldInfo.Value:", value)
			dataByte, err := base64.StdEncoding.DecodeString(value) // 不可用于http传输，否则使用URLEncoding
			if err != nil {
				return err
			}
			rs := crypto.MysqlAESDecrypt(dataByte, []byte(global.ENCRYPTKEY_AES))
			v.FieldByName(fieldInfo.Name).Set(reflect.ValueOf(string(rs)))
		}
	}
	return
}

type EpidemicDictionaryDetail struct {
	ID    uint   `json:"id" gorm:"column:id;"`
	Key   string `json:"key" gorm:"column:key;"`
	Name  string `json:"name" gorm:"column:name;"`
	Value string `json:"value" gorm:"column:value;"`
}

// TableName EpidemicUser 表名
func (EpidemicDictionaryDetail) TableName() string {
	return "epidemic_dictionary_detail"
}
